package com.hyfd.service.mp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hyfd.common.BaseJson;
import com.hyfd.common.utils.DateTimeUtils;
import com.hyfd.common.utils.ExceptionUtils;
import com.hyfd.common.utils.MapUtils;
import com.hyfd.common.utils.ToolValidator;
import com.hyfd.dao.mp.SubmitOrderDao;
import com.hyfd.service.BaseService;

@Service
public class WebSubmitOrderSer extends BaseService{

	public Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	SubmitOrderDao submitOrderDao;//提交订单
	@Autowired
	chargeOrderSer chargeOrderSer;
	
	
	public String webSubmitOrder(HttpServletRequest request){
		String result="";
		Map<String,Object> param = getMaps(request);
		String callbackUrl="baidu.com";
		String type = (String) param.get("type");
		if(type.equals("1")){//单充
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String timeStamp = sdf.format(date);
			UUID uuid = UUID.randomUUID();
			String customerOrderId = uuid.toString().replace("-", "");
			param.put("timeStamp", timeStamp);
			param.put("callbackUrl", callbackUrl);
			param.put("custOrderId", customerOrderId);
			Map<String, Object> map = submitOrder(param);
			result = BaseJson.mapToJson(map);
		}else if(type.equals("2")){//批充
			Map<String,Object> data= new HashMap<String, Object>();
			String terminalName = (String) param.get("custName");
			String scope = (String) param.get("roamType");
			String orderType = (String) param.get("bizType");//1流量2话费
			List<String> phoneNo = new ArrayList<String>();
			String p = (String) param.get("phone");
			String[] split2 = p.split(",");
			for(int i =0 ;i<split2.length;i++){
				String string = split2[i];
				phoneNo.add(string);
			}
			int succ=0;
			int error=0;
			String errorMessage="";
			for(String phoneMeg:phoneNo){
				String[] split = phoneMeg.split("-");
				String phone = split[0];//手机号
				String spec = split[1];//面值
				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
				String timeStamp = sdf.format(date);
				UUID uuid = UUID.randomUUID();
				String customerOrderId = uuid.toString().replace("-", "");
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("custName", terminalName);
				map.put("roamType", scope);
				map.put("bizType", orderType);
				map.put("callbackUrl", callbackUrl);
				map.put("phone", phone);
				map.put("spec", spec);
				map.put("custOrderId", customerOrderId);
				map.put("timeStamp", timeStamp);
				Map<String, Object> submitOrder = submitOrder(map);
				int code = (int) submitOrder.get("code");
				if(code==0){
					succ+=1;
				}else{
					String message = (String) submitOrder.get("message");
					errorMessage="\n"+errorMessage+phone+":"+message+"\n";
					error+=1;
				}
			}
			data.put("success", succ);
			data.put("error", error);
			data.put("errorMessage", errorMessage);
			result = BaseJson.mapToJson(data);
		}
		return result;
	}
	
	/**
	 * 提交订单
	 * */
	public Map<String,Object> submitOrder(Map<String,Object> param){
		Map<String,Object> m = new HashMap<String, Object>();
		int code = -1;
		if(MapUtils.checkEmptyValue(param)&&checkParamIntact(param)){//验证参数
		code = chargeOrderSer.createOrder(param);
		}else{
			code = 1;//参数不全
		}
		String message = "";
		switch(code){
			case 0:
				message = "提交成功";
				break;
			case 1:
				message = "参数不正确";
				break;
			case 2:
				message = "签名错误";
				break;
			case 3:
				message = "该代理商不可用";
				break;
			case 4:
				message = "不支持该手机号段";
				break;
			case 5:
				message = "不允许充值该运营商号段，请联系业务人员获取权限";
				break;
			case 6:
				message = "无法充值该产品";
				break;
			case 7:
				message = "余额不足";
				break;
			case 8:
				message = "订单提交出现异常";
				break;
			case 9:
				message = "折扣已变更，请联系商务";
				break;
			default:
				code = -1;
				message = "状态返回值异常";
				break;
		}
		param.put("code", code);
		param.put("message", message);
		saveSubmitOrder(param);//保存订单
		m.put("code", code);
		m.put("message", message);
		return m;
	}
	
	/**
	 * 判断参数是否完整
	 * @author 
	 * @param param
	 * @return
	 */
	public boolean checkParamIntact(Map<String,Object> param){
		boolean flag = true;
		//校验参数不为空
		if(param.get("custName")==null||param.get("custOrderId")==null||
				param.get("phone")==null||param.get("bizType")==null
				||param.get("spec")==null||param.get("roamType")==null||
				param.get("callbackUrl")==null||param.get("timeStamp")==null){
			flag = false;
		}else{
			//校验参数符合规则
			if (32 != ((String) param.get("custOrderId")).length()
					|| (11 != ((String) param.get("phone")).length() && 13 != ((String) param.get("phone")).length())
					|| (!"1".equals(param.get("bizType")) && !"2".equals(param.get("bizType")))
					|| 256 < ((String) param.get("callbackUrl")).length()
					|| ((!ToolValidator.isMobile((String) param.get("phone")))
							&& (!ToolValidator.isWLWMobile((String) param.get("phone"))))) {
				flag = false;
			}
		}
		return flag;
	}
	
	/**
	 * 保存下家提交的订单
	 * @author 
	 * @param param
	 */
	public void saveSubmitOrder(Map<String,Object> param){
		try{
			String agentName = (String) param.get("custName");//客户名
			String orderId = (String) param.get("custOrderId");//客户订单号
			String submitParam = MapUtils.toString(param);//提交参数
			String phone = (String) param.get("phone");//手机号
			String bizType = (String) param.get("bizType");//订单类型
			String submitDate = DateTimeUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");//提交时间
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", UUID.randomUUID().toString().replace("-", ""));
			map.put("agentName", agentName);
			map.put("orderId", orderId);
			map.put("submitParam", submitParam);
			map.put("resultCode", param.get("code")+":"+param.get("message"));
			map.put("phone", phone);
			map.put("bizType", bizType);
			map.put("submitDate", submitDate);
			submitOrderDao.insertSelective(map);
		}catch(Exception e){
			log.error("保存提交订单出错"+ExceptionUtils.getExceptionMessage(e));
		}
	}
}
